package com.example.blogback.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.blogback.dto.ApiResponse;
import com.example.blogback.dto.ArticleDTO;
import com.example.blogback.entity.Article;
import com.example.blogback.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

@Api(tags = "文章管理接口")
@RestController
@RequestMapping("/api/articles")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @ApiOperation("创建文章")
    @PostMapping
    public ApiResponse<Void> createArticle(@RequestBody ArticleDTO articleDTO) {
        articleService.createArticle(articleDTO);
        return ApiResponse.success();
    }

    @ApiOperation("更新文章")
    @PutMapping
    public ApiResponse<Void> updateArticle(@RequestBody ArticleDTO articleDTO) {
        articleService.updateArticle(articleDTO);
        return ApiResponse.success();
    }

    @ApiOperation("删除文章")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteArticle(@PathVariable @ApiParam("文章ID") Long id) {
        articleService.deleteArticle(id);
        return ApiResponse.success();
    }

    @ApiOperation("设置文章是否推荐")
    @PutMapping("/{id}/featured")
    public ApiResponse<Void> setFeatured(
            @PathVariable @ApiParam("文章ID") Long id,
            @RequestParam @ApiParam("是否推荐") Boolean featured) {
        articleService.setFeatured(id, featured);
        return ApiResponse.success();
    }

    @ApiOperation("获取文章详情")
    @GetMapping("/{id}")
    public ApiResponse<ArticleDTO> getArticleDetail(@PathVariable @ApiParam("文章ID") Long id) {
        return ApiResponse.success(articleService.getArticleDetail(id));
    }

    @ApiOperation("获取文章列表")
    @GetMapping
    public ApiResponse<IPage<Article>> getArticleList(
            @RequestParam(required = false) @ApiParam("分类ID") Long categoryId,
            @RequestParam(defaultValue = "1") @ApiParam("页码") Integer page,
            @RequestParam(defaultValue = "10") @ApiParam("每页数量") Integer size) {
        return ApiResponse.success(articleService.getArticleList(categoryId, page, size));
    }

    @ApiOperation("增加文章浏览量")
    @PutMapping("/{id}/view")
    public ApiResponse<Void> incrementViewCount(@PathVariable @ApiParam("文章ID") Long id) {
        articleService.incrementViewCount(id);
        return ApiResponse.success();
    }
}